
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  
<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/deployment/proxying/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>HTTP Proxying &#8212; Werkzeug Documentation (1.0.x)</title>
    <link rel="stylesheet" href="../../_static/werkzeug.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../../../assets.readthedocs.org/static/css/badge_only.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script async="async" type="text/javascript" src="../../../../../assets.readthedocs.org/static/javascript/readthedocs-doc-embed.js"></script>
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
    <link rel="index" title="Index" href="../../genindex/index.html" />
    <link rel="search" title="Search" href="../../search/index.html" />
    <link rel="next" title="Important Terms" href="../../terms/index.html" />
    <link rel="prev" title="FastCGI" href="../fastcgi/index.html" />
    <link rel="canonical" href="index.html">
  <script>DOCUMENTATION_OPTIONS.URL_ROOT = '../../index.html';</script>
   
  
<!-- RTD Extra Head -->

<!-- 
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="index.html" />

<link rel="stylesheet" href="../../../../../assets.readthedocs.org/static/css/readthedocs-doc-embed.css" type="text/css" />

<script type="text/javascript" src="../../_static/readthedocs-data.js"></script>

<!-- Add page-specific data, which must exist in the page js, not global -->
<script type="text/javascript">
READTHEDOCS_DATA['page'] = "deployment/proxying"
READTHEDOCS_DATA['source_suffix'] = ".rst"
</script>

<script type="text/javascript" src="../../../../../assets.readthedocs.org/static/javascript/readthedocs-analytics.js" async="async"></script>

<!-- end RTD <extrahead> -->
</head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex/index.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex/index.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../terms/index.html" title="Important Terms"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../fastcgi/index.html" title="FastCGI"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">Werkzeug Documentation (1.0.x)</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Application Deployment</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="http-proxying">
<h1>HTTP Proxying<a class="headerlink" href="#http-proxying" title="Permalink to this headline">¶</a></h1>
<p>Many people prefer using a standalone Python HTTP server and proxying that
server via nginx, Apache etc.</p>
<p>A very stable Python server is CherryPy.  This part of the documentation
shows you how to combine your WSGI application with the CherryPy WSGI
server and how to configure the webserver for proxying.</p>
<div class="section" id="creating-a-py-server">
<h2>Creating a <cite>.py</cite> server<a class="headerlink" href="#creating-a-py-server" title="Permalink to this headline">¶</a></h2>
<p>To run your application you need a <cite>start-server.py</cite> file that starts up
the WSGI Server.</p>
<p>It looks something along these lines:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">cherrypy</span> <span class="k">import</span> <span class="n">wsgiserver</span>
<span class="kn">from</span> <span class="nn">yourapplication</span> <span class="k">import</span> <span class="n">make_app</span>
<span class="n">server</span> <span class="o">=</span> <span class="n">wsgiserver</span><span class="o">.</span><span class="n">CherryPyWSGIServer</span><span class="p">((</span><span class="s1">&#39;localhost&#39;</span><span class="p">,</span> <span class="mi">8080</span><span class="p">),</span> <span class="n">make_app</span><span class="p">())</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">server</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="k">except</span> <span class="ne">KeyboardInterrupt</span><span class="p">:</span>
    <span class="n">server</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
</pre></div>
</div>
<p>If you now start the file the server will listen on <cite>localhost:8080</cite>.  Keep
in mind that WSGI applications behave slightly different for proxied setups.
If you have not developed your application for proxying in mind, you can
apply the <a class="reference internal" href="../../middleware/proxy_fix/index.html#werkzeug.middleware.proxy_fix.ProxyFix" title="werkzeug.middleware.proxy_fix.ProxyFix"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProxyFix</span></code></a> middleware.</p>
</div>
<div class="section" id="configuring-nginx">
<h2>Configuring nginx<a class="headerlink" href="#configuring-nginx" title="Permalink to this headline">¶</a></h2>
<p>As an example we show here how to configure nginx to proxy to the server.</p>
<p>The basic nginx configuration looks like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>location / {
    proxy_set_header        Host $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass              http://127.0.0.1:8080;
    proxy_redirect          default;
}
</pre></div>
</div>
<p>Since Nginx doesn’t start your server for you, you have to do it by yourself.  You
can either write an <cite>init.d</cite> script for that or execute it inside a screen
session:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ screen
$ python start-server.py
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
  <span id="sidebar-top"></span>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  
    
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/werkzeug.png" alt="Logo"/>
            </a></p>
  

  <h3>Contents</h3>
  <ul>
<li><a class="reference internal" href="#">HTTP Proxying</a><ul>
<li><a class="reference internal" href="#creating-a-py-server">Creating a <cite>.py</cite> server</a></li>
<li><a class="reference internal" href="#configuring-nginx">Configuring nginx</a></li>
</ul>
</li>
</ul>
<h3>Navigation</h3>
<ul>
  <li><a href="../../index.html">Overview</a>
    <ul>
      <li><a href="../index.html">Application Deployment</a>
        <ul>
          <li>Previous: <a href="../fastcgi/index.html" title="previous chapter">FastCGI</a>
          <li>Next: <a href="../../terms/index.html" title="next chapter">Important Terms</a></ul>
      </li>
    </ul>
  </li>
</ul>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://werkzeug.palletsprojects.com/en/1.0.x/search/" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  
    <div class="footer" role="contentinfo">
        &#169; Copyright 2007 Pallets.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.5.
    </div>
    <script>
      if (typeof READTHEDOCS_DATA !== 'undefined') {
        if (!READTHEDOCS_DATA.features) {
          READTHEDOCS_DATA.features = {};
        }
        READTHEDOCS_DATA.features.docsearch_disabled = true;
      }
    </script>

  </body>

<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/deployment/proxying/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
</html>